//引入fs
let fs = require('fs')

//写三个读取文件的函数，返回的都是promise对象
 function read1() {
    return new Promise((resolve, reject) => {
        fs.readFile('./ES6_promise/resources/为学.md',(err,data)=>{
            if (err) reject(err)
            resolve(data)
        })
    })
}

function read2() {
    return new Promise((resolve, reject) => {
        fs.readFile('./ES6_promise/resources/观书有感.md',(err,data)=>{
            if (err) reject(err)
            resolve(data)
        })
    })
}

function read3() {
    return new Promise((resolve, reject) => {
        fs.readFile('./ES6_promise/resources/插秧诗.md',(err,data)=>{
            if (err) reject(err)
            resolve(data)
        })
    })
}

//在async中使用await变成异步，同时使用try catch捕获异常
//不管何种情况,readAll返回的都是promise对象，方便后续链式调用
async  function readAll() {
    try {
        let result1 = await    read1()
        let result2 = await    read2()
        let result3 = await    read3()

        return new Promise(resolve => {
            resolve(result1+"\r\n"+result2+"\r\n"+result3)
        })
    }catch (e){
        return new Promise((resolve, reject) => {
            reject(e)
        })
    }

}

//调用readAll函数，返回promise对象，使用then方法进行链式调用
readAll().then(value => {
    console.log(value.toString())
},reason => {
    console.log(reason)
})

